package concurrent.demo3;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 栅栏示例
 *
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2019/4/1/001 下午 8:21
 */
public class CycWork implements Runnable {

  private CyclicBarrier cyclicBarrier;
  private String name;

  public CycWork(CyclicBarrier cyclicBarrier, String name) {
    this.cyclicBarrier = cyclicBarrier;
    this.name = name;
  }

  @Override
  public void run() {
    System.out.println(this.name + "正在打桩");
    try {
      Thread.sleep(new Random().nextInt(5000));
      System.out.println(name + "不容易，终于把桩打完了");
      cyclicBarrier.await();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (BrokenBarrierException e) {
      e.printStackTrace();
    }
    System.out.println(name + "桩都打完了");
  }
}
